package com.xoms.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xoms.admin.controller.res.BaseResult;
import com.xoms.admin.controller.res.ListResult;
import com.xoms.admin.controller.res.ObjectResult;
import com.xoms.admin.controller.res.bean.RProCheckBean;
import com.xoms.admin.controller.res.bean.RProCheckProInfoBean;
import com.xoms.admin.controller.springmvc.req.DelCheckParam;
import com.xoms.admin.controller.springmvc.req.EditCheckParam;
import com.xoms.admin.controller.springmvc.req.GetCheckDetailsParam;
import com.xoms.admin.controller.springmvc.req.GetProChecksParam;
import com.xoms.admin.controller.springmvc.req.SubCheckParam;
import com.xoms.admin.model.AccountBean;
import com.xoms.admin.service.ProCheckService;
import com.xoms.admin.service.exception.ServiceException;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.NumUtil;

@Component
public class ProCheckController {
	

	@Autowired
	ProCheckService proCheckServiceImpl;
	
	/**
	 * 获取盘库列表
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProCheckBean> getChecks(GetProChecksParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProCheckBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");

			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			String startTime = param.getStartTime();// 起始时间
			String endTime = param.getStartTime();// 终止时间
			String storeId = param.getStoreHouseId();// 仓库id
			String key = param.getKey();// 盘库搜索关键字
			
			// 获取盘库列表
			PageResult<RProCheckBean> resData = proCheckServiceImpl.getChecks(account, page, PageSize,
					startTime, endTime, storeId, key);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 盘库详情
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProCheckProInfoBean> getCheckDetails(GetCheckDetailsParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProCheckProInfoBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");

			String cid = param.getCid();// 盘库id
			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			int paging = NumUtil.toInt(param.getPaging(), 0);// 默认不分页
			// 获取采购详情列表
			PageResult<RProCheckProInfoBean> resData = proCheckServiceImpl.getCheckDetails(account, page, PageSize,
					cid, paging);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 编辑/新建盘库
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RProCheckBean> editCheck(EditCheckParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RProCheckBean> result = new ObjectResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String cid = param.getCid();
			String storeHouseId = param.getStoreHouseId();
			String remark = param.getRemark();
			String details = param.getDetails();
			int isSubmit = NumUtil.toInt(param.getIsSubmit(), 1);// 是否直接提交1保存2直接提交

			// 成功后返回编辑后盘库信息
			RProCheckBean data = proCheckServiceImpl.edit(account, cid, storeHouseId, remark, details, isSubmit);
			result.setData(data);
			result.setFlag(data != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 删除盘库信息
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult delCheck(DelCheckParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String cid = param.getCid();
			boolean flag = proCheckServiceImpl.del(account, cid);// 删除盘库信息
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 提交盘库审核
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult submitCheck(SubCheckParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String cid = param.getCid();
			boolean flag = proCheckServiceImpl.submitCheck(account, cid);// 提交审核
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

}
